package com.fone.player.activity.local;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.text.TextUtils;

import com.fone.player.entity.MediaFile;
import com.fone.player.entity.PlayRecord;
import com.fone.player.util.L;

public class SortListUtil {

	private static String TAG="SortListUtil";
	// 排序
	public static final int SORT_DATE_ASC = 2;
	public static final int SORT_DATE_DESC = 3;
	public static final int SORT_NAME_ASC = 0;
	public static final int SORT_NAME_DESC = 1;

	private static List<MediaFile> numList;
	private static List<MediaFile> letterList;
	private static List<MediaFile> normalList;

	/**
	 * 
	 * 对集合进行排序
	 * 
	 * @param list
	 * @param orderType
	 *            排序类型：按文件名排序（升序SORT_NAME_ASC、倒序SORT_NAME_DESC） 按日期排序
	 *            （升序SORT_DATE_ASC 、倒序SORT_DATE_DESC）
	 * @param isFolder
	 *            是否是文件夹
	 * @return void
	 * @throws
	 */
	public static void sortList(List<MediaFile> list, int orderType,
			boolean isFolder) {
		divisiveList(list);
		FileNameComparator Comparator = new FileNameComparator();
		switch (orderType) {
		case SORT_NAME_ASC:
			// Collections.sort(list, new FileNameComparator());
			Collections.sort(numList, Comparator);
			Collections.sort(letterList, Comparator);
			Collections.sort(normalList, Comparator);
			list.clear();
			list.addAll(numList);
			list.addAll(letterList);
			list.addAll(normalList);
			break;

		case SORT_NAME_DESC:
			// Collections.sort(list, new FileNameComparator());
			Collections.sort(numList, Comparator);
			Collections.sort(letterList, Comparator);
			Collections.sort(normalList, Comparator);
			list.clear();
			list.addAll(numList);
			list.addAll(letterList);
			list.addAll(normalList);
			Collections.reverse(list);
			break;

		case SORT_DATE_ASC:
			if (isFolder) {
				Collections.sort(list, new Comparator<MediaFile>() {

					@Override
					public int compare(MediaFile object1, MediaFile object2) {
						long result = new File(object1.getMediaFilePath())
								.lastModified()
								- new File(object2.getMediaFilePath())
										.lastModified();
						if (result > 0) {
							return -1;
						} else if (result < 0) {
							return 1;
						} else
							return 0;
					}

				});
			} else {
				Collections.sort(list, new Comparator<MediaFile>() {

					@Override
					public int compare(MediaFile object1, MediaFile object2) {
						long result = object1.getMediaFileDateModified()
								- object2.getMediaFileDateModified();
						if (result > 0) {
							return -1;
						} else if (result < 0) {
							return 1;
						} else
							return 0;
					}

				});
			}
			break;
		case SORT_DATE_DESC:
			if (isFolder) {
				Collections.sort(list, new Comparator<MediaFile>() {

					@Override
					public int compare(MediaFile object1, MediaFile object2) {
						long result = new File(object1.getMediaFilePath())
								.lastModified()
								- new File(object2.getMediaFilePath())
										.lastModified();
						if (result > 0) {
							return 1;
						} else if (result < 0) {
							return -1;
						} else
							return 0;
					}

				});
			} else {
				Collections.sort(list, new Comparator<MediaFile>() {

					@Override
					public int compare(MediaFile object1, MediaFile object2) {
						long result = object1.getMediaFileDateModified()
								- object2.getMediaFileDateModified();
						if (result > 0) {
							return 1;
						} else if (result < 0) {
							return -1;
						} else
							return 0;
					}

				});
			}
			break;

		default:
			break;
		}
		numList = null;
		letterList = null;
		normalList = null;
	}

	public static void sortEncryptList(List<MediaFile> list, final int orderType) {
		if (orderType == SORT_DATE_DESC) {
			Collections.sort(list, new Comparator<MediaFile>() {

				@Override
				public int compare(MediaFile object1, MediaFile object2) {
					long result = object1.getMediaFileEncryptTime()
							- object2.getMediaFileEncryptTime();
					L.i(TAG, "time1:"+object1.getMediaFileEncryptTime());
					L.i(TAG, "time2:"+object2.getMediaFileEncryptTime());

					if (result > 0) {
						return -1;
					} else if (result < 0) {
						return 1;
					} else {
						return 0;
					}
				}
			});
		} else {
			Collections.sort(list, new Comparator<MediaFile>() {

				@Override
				public int compare(MediaFile object1, MediaFile object2) {
					long result = object1.getMediaFileEncryptTime()
							- object2.getMediaFileEncryptTime();

					if (result > 0) {
						return 1;
					} else if (result < 0) {
						return -1;
					} else {
						return 0;
					}
				}
			});
		}
	}

	private static void divisiveList(List<MediaFile> list) {
		numList = new ArrayList<MediaFile>();
		letterList = new ArrayList<MediaFile>();
		normalList = new ArrayList<MediaFile>();
		for (int i = 0; i < list.size(); i++) {
			MediaFile file = list.get(i);
			String nameSuffix = file.getMediaFileNameSuffix();
			if (TextUtils.isEmpty(nameSuffix)) {
				continue;
			}
			char c = nameSuffix.charAt(0);
			boolean b = Character.isDigit(c);
			if (b) {
				numList.add(file);
			} else {

				if (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z') {
					letterList.add(file);
				} else {
					normalList.add(file);
				}

			}
		}
	}

	public static void sortHistoryRecord(List<PlayRecord> list) {
		Collections.sort(list, new Comparator<PlayRecord>() {

			@Override
			public int compare(PlayRecord object1, PlayRecord object2) {
				long result = object1.getPlayRecordCreateTime()
						- object2.getPlayRecordCreateTime();
				if (result > 0) {
					return -1;
				} else if (result < 0) {
					return 1;
				} else
					return 0;
			}

		});
	}

}
